﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using Lime49;
using Lime49.Mobile.UI;
using LockCrypt.Core;

namespace LockCrypt.Mobile {
    /// <summary>
    /// A form used to prompt for the name of a field.
    /// </summary>
    public partial class FrmEditField : NonFullscreenForm {
        private IEnumerable<IField> existingFields;
        private bool cancelDialog = false,
                     editing = false;

        /// <summary>
        /// Gets or sets the name of the field.
        /// </summary>
        /// <value>The name of the field.</value>
        public string FieldName {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }

        /// <summary>
        /// Initializes a new <see cref="FrmEditField"/> to add a field.
        /// </summary>
        public FrmEditField(Form owner, IEnumerable<IField> existingFields) {
            InitializeComponent();
            this.Owner = owner;
            this.existingFields = existingFields;
            btnOk.Click += (s, e) => {
                if(ValidateField()) {
                    this.DialogResult = DialogResult.OK;
                }
            };
            btnCancel.Click += (s, e) => {
                cancelDialog = true;
                this.DialogResult = DialogResult.Cancel;
            };
            this.Closing += delegate(object sender, CancelEventArgs e) {
                if(!cancelDialog) {
                    if(ValidateField()) {
                        this.DialogResult = DialogResult.OK;
                    }
                }
            };
            this.Text = I18nUtils.GetString("Strings", "AddField");
            this.Activated += (s, e) => {
                txtName.SelectAll();
                txtName.Focus();
            };
        }
        
        /// <summary>
        /// Initializes a new <see cref="FrmEditField"/> to edit a field.
        /// </summary>
        /// <param name="fieldName">Name of the field to edit.</param>
        public FrmEditField(Form owner, IEnumerable<IField> existingFields, string fieldName) 
            : this(owner, existingFields) {
            this.Text = I18nUtils.GetString("Strings", "EditField");
            txtName.Text = fieldName;
            editing = true;
        }

        /// <summary>
        /// Checks that the type name is not null and that no type with the name entered exists. Displays a messagebox if errors are detected.
        /// </summary>
        /// <returns></returns>
        private bool ValidateField() {
            if(string.IsNullOrEmpty(txtName.Text)) {
                MessageBox.Show(I18nUtils.GetString("Errors", "FieldNameBlank"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                txtName.SelectAll();
                txtName.Focus();
                return false;
            } else if(!editing && existingFields.Any(f=>string.Equals(f.Name, txtName.Text.Trim(), StringComparison.InvariantCultureIgnoreCase))) {
                MessageBox.Show(I18nUtils.GetString("Errors", "FieldExists"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                txtName.SelectAll();
                txtName.Focus();
                return false;
            }
            return true;
        }
    }
}